
var zg = {
	progressValue : 0,		//进度百分数
	progressWidth: "480px",	//进度条宽度
	progressColor: "red",	//进度条颜色
	modal: true,			//是否是模态框
	progressBorderColor: "#ffffcc",		//进度条边框颜色
	progressBackgroundColor: "#ffffff",	//进度条背景色
	progressBorderWidth: "20px",		//进度条边框宽度
	progressHeight: "20px",				//进度条高度
	progressFontColor: "#000000",				//进度条文字颜色
	progressFontSize: "16px",		
	text: "",			//进度条
	textColor: "black",
	textSize: "16px",
	textPosition: "left",	//提示文字的位置 left top right
	progress: function(str,value){
		if(typeof(str) == "string"){
			switch (str){
			case "open":
				this.progressInit();
				this.setValue();
			break;
			case "close":
				document.body.removeChild(this.backDiv);
				if (this.overDiv) {
					document.body.removeChild(this.overDiv);
				}
			break;
			case "set":
				this.progressValue = value;
				this.setValue();
			break;
			case "get": 
				return this.progressValue;
			break;
			}
		}else if(typeof(str) == "number"){
			this.progressValue = parseFloat(str);
			this.setValue();
		}else if(str == null){
			
		}else if(typeof(str) == "object"){
			this.progressValue = str.value != null ? str.value: 0;
			this.progressColor = str.color != null ? str.color : "red";
			this.progressBackgroundColor = str.backgroundColor != null ? str.backgroundColor : "#ffffcc";
			this.progressWidth = str.width != null ? str.width : "480px";
			this.progressHeight = str.height != null ? str.height : "20px";
			this.modal = str.modal != null ? str.modal : true;
			this.progressFontColor = str.fontColor != null ? str.fontColor : "#000000";
			this.progressFontSize = str.fontSize != null ? str.fontSize : "16px";
			this.progressBorderWidth = str.marginWidth != null ? str.marginWidth : "20px";
			this.progressBorderColor = str.marginColor != null ? str.marginColor : "#fbf399";
			this.text = str.text != null ? str.text : "";
			this.textPosition = str.textPosition != null ? str.textPosition : "left";
			this.textColor = str.textColor != null ? str.textColor : "black";
			this.textSize = str.textSize != null ? str.textSize : "16px";
		}else{
			
		}
	},
	overDiv: null,	//遮罩层
	backDiv: null,	//进度条内容层
	outDiv: null,	//进度条外边框
	innerDiv: null,	//进度条内边框
	modalDiv: function(){
		if(this.modal){
			this.overDiv = document.createElement("div");
			document.body.appendChild(this.overDiv);
			this.overDiv.className = "modalDiv";
		}
	},
	progressInit: function(){
		this.modalDiv();
		this.backDiv = document.createElement("div");
		document.body.appendChild(this.backDiv);
		this.backDiv.className = "contentDiv";
		this.backDiv.style.marginLeft="-"+parseInt(this.progressWidth)/2+"px";
		this.backDiv.style.backgroundColor = this.progressBorderColor;
		this.backDiv.style.padding = this.progressBorderWidth;
		if (this.text && this.text != "") {
			var textDiv = document.createElement("div");
			this.backDiv.appendChild(textDiv);
			textDiv.innerHTML = this.text;
			textDiv.style.color = this.textColor;
			textDiv.style.fontSize = this.textSize;
			textDiv.style.float = this.textPosition;
		}

		this.outDiv = document.createElement("div");
		this.backDiv.appendChild(this.outDiv);
		this.outDiv.style.width=this.progressWidth;
		this.outDiv.style.border="1px solid black";
		this.outDiv.style.margin="auto";
		this.outDiv.style.borderRadius="5px";
		this.outDiv.style.backgroundColor=this.progressBackgroundColor;
		if (this.text && this.textPosition == "left") {
			this.outDiv.style.float = "right";
		}

		this.innerDiv = document.createElement("div");
		this.outDiv.appendChild(this.innerDiv);
		this.innerDiv.style.height=this.progressHeight;
		this.innerDiv.style.backgroundColor = this.progressColor;
		this.innerDiv.style.borderRadius="4px";
		this.innerDiv.style.width = "0px";
		this.innerDiv.innerHTML = "0%";
		this.innerDiv.style.color = this.progressFontColor;
		this.innerDiv.style.fontSize = this.progressFontSize;
	},
	setValue:function(){
		this.innerDiv.style.width = (this.progressValue/100*parseInt(this.progressWidth))+"px";
		if (parseFloat(this.progressFontSize) <= parseFloat(this.progressHeight)) {
			this.innerDiv.innerHTML = this.progressValue+"%";
		}else{
			var top = parseFloat(this.progressHeight)-2;
			this.innerDiv.innerHTML = "<span style='top:"+top+"px;position:relative;'>"+this.progressValue+"%</span>";
		}
	},
	//上面是进度条代码,下面是等待框代码
	waitDiv: null,
	wait: function(str, value){
		if("open" == str){
			if (value != null) {
				this.modal = value;
			}
			this.waitInit();
		}else if("close" == str){
			document.body.removeChild(this.waitDiv);
			if (this.overDiv) {
				document.body.removeChild(this.overDiv);
			}
		}else if(typeof(str) == "object"){
			this.modal = str.modal != null ? str.modal : true;
		}
	},
	waitInit: function(){
		this.modalDiv();
		this.waitDiv = document.createElement("div");
		document.body.appendChild(this.waitDiv);
		this.waitDiv.className = "waitloading";
		this.waitDiv.innerHTML = "正在全力加载中...";
	},
	//下面是时钟代码
	slen: 170,	//指针长度
	mlen: 135,
	hlen: 100,
	swidth: 2,	//指针宽度
	mwidth: 3,
	hwidth: 4,
	s: 0,		//时分秒数
	m: 0,
	h: 0,
	clockCanvas: null,		//时钟画布
	clockColor: "black",	//时钟颜色
	secondColor: "black",	//秒针颜色
	minuteColor: "black",	//分针颜色
	hourColor: "black",		//时针颜色
	scale: 1,				//缩放比例
	zoom: "100%",			//画布缩放比例
	clockTargetId : null,		//目标标签id值
	number:true,			//是否显示时钟数字
	clock: function(str){
		if ("open" == str) {
			this.drawClock();
		}else if ("close" == str) {
			document.body.removeChild(this.clockCanvas);
		}else if (typeof(str) == "object") {
			this.clockColor = str.clockColor != null ? str.clockColor : "black";
			this.secondColor = str.secondColor != null ? str.secondColor : "black";
			this.minuteColor = str.minuteColor != null ? str.minuteColor : "black";
			this.hourColor = str.hourColor != null ? str.hourColor : "black";
			this.scale = str.scale != null ? str.scale : 1;
			this.zoom = str.zoom != null ? str.zoom : "100%";
			this.clockTargetId = str.targetId != null ? str.targetId : null;
			this.number = str.number != null ? str.number : true;
		}
	},
	drawClock: function(){
		clockCanvas = document.createElement("canvas");
		//document.body.appendChild(clockCanvas);
		if ("string" == typeof(this.clockTargetId)) {
			if (document.getElementById(this.clockTargetId) == null) {
				throw "targetId is Invalid";
			}else{
				document.getElementById(this.clockTargetId).appendChild(clockCanvas);
				clockCanvas.style.position = "relative";
			}
		}else{
			document.body.appendChild(clockCanvas);
			clockCanvas.style.position = "absolute";
		}
		clockCanvas.width = 500;
		clockCanvas.height = 500;
		clockCanvas.style.zoom = this.zoom;
		clockCanvas.className = "zgCanvas";
		var ctx = clockCanvas.getContext("2d");
		ctx.scale(this.scale,this.scale);	//缩放
		ctx.beginPath();
		ctx.translate(250,250);	//重新定义坐标
		ctx.arc(0,0,4,0,2*Math.PI);	//画中心圆点
		ctx.fillStyle = this.clockColor
		ctx.fill();
		ctx.closePath();
		if (this.number) {
			ctx.font="20px Georgia";
			ctx.fillText("12",-10,-205);
			ctx.fillText("1",102,-177);
			ctx.fillText("2",177,-102);
			ctx.fillText("3",205,4);
			ctx.fillText("4",176,111);
			ctx.fillText("5",101,189);
			ctx.fillText("6",-5,220);
			ctx.fillText("7",-110,189);
			ctx.fillText("8",-188,111);
			ctx.fillText("9",-215,4);
			ctx.fillText("10",-196,-101);
			ctx.fillText("11",-117,-179);
		}

		ctx.beginPath();
		ctx.arc(0,0,200,0,2*Math.PI);	//画外圈
		ctx.strokeStyle = this.clockColor;
		ctx.stroke();
		ctx.closePath();

		ctx.beginPath();				//画刻度
		ctx.rotate(-Math.PI/2);			//逆时针旋转90°
		ctx.save();
		for(var i=0; i<60; i++){
			if(i % 5 == 0){
				ctx.fillRect(180,-2.5,20,5);
			}else{
				ctx.fillRect(190,-2.5,10,2.5);
			}
			ctx.rotate(6 * Math.PI/180);	//顺时针旋转6°
		}
		ctx.closePath();
		setInterval(this.clockRefresh,1000,ctx);
	},
	clockRefresh: function(ctx){
		ctx.restore();
		ctx.save();
		ctx.rotate(zg.s * Math.PI/30);
		ctx.clearRect(5,-2,zg.slen+1,zg.swidth+2);		//擦除秒针
		ctx.restore();
		ctx.save();
		ctx.rotate(zg.m * Math.PI/30);
		ctx.clearRect(5,-2,zg.mlen+1,zg.mwidth+2);		//擦除分针
		ctx.restore();
		ctx.save();
		ctx.rotate(zg.h * Math.PI/6 + (zg.m/60 * Math.PI/6));
		ctx.clearRect(5,-2,zg.hlen+1,zg.hwidth+2);		//擦除时针

		var time = new Date();	//获取当前时间
		h = zg.h = time.getHours();
		m = zg.m = time.getMinutes();
		s = zg.s = time.getSeconds();
		
		ctx.restore();
		ctx.save();
		ctx.rotate(h * Math.PI/6 + (m/60 * Math.PI/6));
		ctx.fillStyle = zg.hourColor;
		ctx.fillRect(5,-1,zg.hlen,zg.hwidth);			//画时针
		ctx.restore();
		ctx.save();
		ctx.rotate(m * Math.PI/30);
		ctx.fillStyle = zg.minuteColor;
		ctx.fillRect(5,-1,zg.mlen,zg.mwidth);			//画分针
		ctx.restore();
		ctx.save();
		ctx.rotate(s * Math.PI/30);
		ctx.fillStyle = zg.secondColor;
		ctx.fillRect(5,-1,zg.slen,zg.swidth);			//画秒针
	},
	//下面是按钮内的等待框
	mycanvas:null,		//等待框画布
	rwidth:6,			//默认线条宽度
	lwidth:4,			//旋转线条宽度
	disabled : true,	//是否禁用按钮
	waitColor: "black",	//等待框颜色
	btnWaitinit: function(){
		this.mycanvas = document.createElement("canvas");
		if (this.targetId != null) {
			var ele = document.getElementById(this.targetId);
			if (ele == null) {
				throw "targetId is invalid!";
			}else{
				ele.appendChild(this.mycanvas);
			}
		}else{
			document.body.appendChild(this.mycanvas);
		}
		this.mycanvas.width = 100;
		this.mycanvas.height = 100;
		if (this.targetId == null) {
			this.mycanvas.style.position = "absolute";
			this.mycanvas.className = "btnWaitimg_p";
		}else{
			this.mycanvas.style.position = "relative";
			this.mycanvas.className = "btnWaitimg_r";
		}
		this.mycanvas.style.zoom = this.zoom;
		
	},
	btnWait : function(str, targetId){
		if(str == "open"){
			if (targetId != null) {
				this.zoom = "20%";
				this.targetId = targetId;
			}
			if (!this.mycanvas) {
				this.btnWaitinit();
				this.btnWaitStoke();
				if (this.disabled && this.targetId) {
					document.getElementById(this.targetId).disabled = true;
				}
			}
		}else if (str == "close") {
			if (this.targetId != null) {
				document.getElementById(this.targetId).removeChild(this.mycanvas);
			}else{
				document.body.removeChild(this.mycanvas);
			}
			if (this.disabled && this.targetId) {
				document.getElementById(this.targetId).disabled = false;
			}
			this.mycanvas = null;
		}else if("object" == typeof(str)){
			this.disabled = str.disabled != null ? str.disabled : true;
			this.targetId = str.targetId != null ? str.targetId : null;
			this.zoom = str.zoom != null ? str.zoom : "20%";
			this.waitColor = str.waitColor != null ? str.waitColor : "black";
		}
	},
	btnWaitStoke:function(){
		ctx=this.mycanvas.getContext('2d');
		ctx.scale(1, 1);
		ctx.beginPath();
		ctx.translate(50,50);	//重新定义0点坐标
		//ctx.save();
		this.btnDrawGrayLine(ctx);
		ctx.save();
		setTimeout(this.btnDrawLine,50,this.rwidth,ctx);
	},
	btnDrawGrayLine: function(ctx){
		for(var i=0; i<16; i++){
			ctx.rotate(i * 22.5 * Math.PI / 180); //旋转22.5°
			ctx.fillStyle = this.waitColor;
			ctx.fillRect(10,-this.lwidth/2,35,this.lwidth);
		}
	},
	btnDrawLine: function(rwidth,ctx){
		zg.btnDrawGrayLine(ctx);
		ctx.restore();
		ctx.save();
		ctx.clearRect(9,-rwidth+2,37,rwidth+3);
		ctx.restore();
		ctx.rotate(22.5 * Math.PI / 180); //旋转22.5°
		ctx.save();
		ctx.fillStyle = zg.waitColor;
		ctx.fillRect(10,-rwidth/2,35,rwidth);		//画蓝色矩形
		setTimeout(zg.btnDrawLine,50,rwidth,ctx);
	}
	
};
zg.ajax = function(url,data,callback,dataType){
	$.ajax({
		type : "POST",
		url : url,
		data : data,
		dataType : dataType,
		cache : false,
		async : true,
		success : function(r) {
			callback(r);
		},
		error : function() {
			bootbox.alert("请求出现异常，请稍后再试！");
		}
	});
}
zg.showModalMessage = function (msg, url){
	bootbox.dialog({  
		title: "提示",  
		message: msg != null ? msg : "请传递需要显示的消息",  
		size: "small",
		buttons: { 
			OK: {  
				label: "OK",  
				className: "btn-primary",  
				callback: function () {  
					//确定
					if (url != null && url == true) {
						location.reload();
					}else if(url != null && url != true){
						location.href = url;
					}
				}  
			}  
		}  
	});
}
zg.showModalMessage2 = function (code,msg, url){
	var div = document.createElement("div");
	$(div).html(msg);
	$(div).css("background","url('') repeat-x scroll left top #fff");
	$(div).css("position","absolute").width(300);
	if (code == "success") {
		$(div).css("background-color","lightgreen")
		$(div).css("color","darkgreen").css("border-color","#50C24E");
	}else if(code == "error"){
		$(div).css("background-color","red")
		$(div).css("color","#fff").css("border-color","darkred");
	}
	$(div).css("-webkit-border-radius","5px").css("-moz-border-radius","5px").css("border-radius","5px");
	$(div).css("font-weight","normal").css("font-size","16px");
	$(div).css("box-shadow","0 2px 4px rgba(0, 0, 0, 0.2)");
	$(div).css("padding","4px").css("text-align","center");
	var v_h = window.innerHeight;
	var v_w = window.innerWidth;
	$(div).css({ left: (v_w/2-150)+"px", top: "260px" });
	$(div).hide();
	$(div).fadeIn(200,function(){
		setTimeout(function(){$(div).fadeOut(500,function(){
			document.body.removeChild(div);
			if (url != null && url == true) {
				location.reload();
			}else if(url != null && url != true){
				location.href = url;
			}
		});},500);
	});
	document.body.appendChild(div);
}

zg.alert = function(code, msg, url){
	if (code == "success" || code == "error") {
		zg.showModalMessage2(code,msg, url);
	}else{
		zg.showModalMessage(code, msg);
	}
}
/**
 * 设置cookies,生存时间为1个月
 */
zg.setCookie=function(name,value){
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime()+Days*24*60*60*1000);
	document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
};
/**
 * 设置cookie，生存时间为自定义，单位毫秒
 */
zg.setCookie=function(name,value,time){
	var exp = new Date();
	exp.setTime(exp.getTime()+parseInt(time));
	document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
};
/**
 * 获取cookies
 */
zg.getCookie=function(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	}else{
		return null;
	}
};
/**
 * 删除cookies
 */
zg.delCookie=function(name){
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = zg.getCookie(name);
	if(cval != null){
		document.cookie=name+"="+cval+";expires="+exp.toGMTString();
	}
};